package com.enqualcomm.kids.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.enqualcomm.kids.R;

/**
 * the SlidingButton Customer view
 */
public class SlidingButton extends ImageButton {

   private Bitmap slide_ku;
    private int mOriginMarginleft = 0;

    public SlidingButton(Context context) {
        super(context);
    }

    public SlidingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlidingButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


    private int mParentWidth = 0;

    private int eventX ;

    private ISlidingListener mSlidingListener;

    public void setSlidingListener(ISlidingListener listener) {
        mSlidingListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                  eventX = (int)event.getX();
//                handlerMoveEvent(event);
                break;
            case MotionEvent.ACTION_MOVE:
                handlerMoveEvent(event);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                handlerUpEvent(event);
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
    private  int marginLeft;
    private void handlerMoveEvent(MotionEvent event) {
        mParentWidth = ((ViewGroup) this.getParent()).getWidth();
        int x = (int) event.getX();
//        Log.v("Aaron","touch x = " + x + " getLeft = " +getLeft() + " x dist = " + (x - eventX) );
         marginLeft = getLeft()+(x - eventX);
//         int marginLeft = getLeft()+eventX;

        Log.v("Aaron","margin left = " + marginLeft);
        //不做中间的值的判断，而使用最大和最小的判断，是因为，当快速滑动到时候两个evevtX的距离会很远，会瞬间超过最大值，
        //此时则不会设置margin了，也就会出现滑不到最右端的bug。所以必须这样处理。
        if(marginLeft < mOriginMarginleft ) {
            marginLeft = mOriginMarginleft;
        } else if(marginLeft > mParentWidth - getWidth()) {
            marginLeft = mParentWidth - getWidth();
        }
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)getLayoutParams();
        lp.leftMargin =marginLeft;
        setLayoutParams(lp);
        //为什么不能使用上面这句，按照常规的逻辑应该为eventX重新赋值的，但是如果加上上面那句就会发现有问题了，弄清楚这个也就弄处理它是怎么滑动的了。
        // x每次的值是相对于当前view上的，move操作会让x1变化为x2，然后变化的量（x2-x1）将其设置为view的margin，这时view位置变化了，
        // view变化后x2的值实际相当于未移动view的x1的值，然后在进行move，又发生偏移，回到上面的逻辑，也就是说偏移量margin相当于重置了x的值。
        // 所以不需要重新赋值，这也是view变化的巧妙之处。
//      eventX = x ;

        // 下面这种写法，在快速滑动的过程中会有问题。
//        int marginLeft = getLeft()+(x - eventX);
//        if(marginLeft >=mOriginMarginleft && marginLeft + getWidth() <= mParentWidth) {
//            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)getLayoutParams();
//            lp.leftMargin = getLeft()+(x - eventX);
//            setLayoutParams(lp);
//            eventX = x ;
//        }
//
    }

    private void handlerUpEvent(MotionEvent event) {
        mParentWidth = ((ViewGroup) this.getParent()).getWidth();
        if (this.getLeft() + getWidth() > mParentWidth - getWidth() / 2) {
            //达到最右端，通知已经可以触发相应的事件了。
            fireSlidingSuccessListener();
            startAnimation(50);
        } else {
            //没有到最右端，则将其自动滑动到原始的位置。
            startAnimation(200);
        }
    }

    private void fireSlidingSuccessListener() {
        if(mSlidingListener != null){
            mSlidingListener.slidingSuccess();
        }
    }

    private void startAnimation(int time) {
        TranslateAnimation trans =
                new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -this.getLeft(),
                        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
        trans.setDuration(time);
        trans.setInterpolator(new AccelerateInterpolator());
        trans.setAnimationListener(new SlidingAnimationListener(this));
        startAnimation(trans);
    }

    private class SlidingAnimationListener implements Animation.AnimationListener {

        private SlidingButton but;

        public SlidingAnimationListener(SlidingButton button) {
            this.but = button;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            rePosition();
            but.clearAnimation();
        }

        private void rePosition() {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) but.getLayoutParams();
            lp.leftMargin = 0;
            but.setLayoutParams(lp);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

    }

    public interface  ISlidingListener {

        public void slidingSuccess();
    }

}
